﻿using System;
using Validity.Localization;

namespace Validity.Rules.Decorators
{
  public class CustomMessageRuleDecorator<TObject, TValue>
      : RuleDecorator<TObject, TValue>
  {
    private readonly Func<TObject, TValue, String> _messageFactory;

    public CustomMessageRuleDecorator(IRule<TObject, TValue> rule, Func<TObject, TValue, String> messageFactory)
      : base(rule)
    {
      Verify.NotNull(messageFactory);

      _messageFactory = messageFactory;
    }

    public override RuleViolation GetRuleViolation(TObject obj, TValue value)
    {
      return new RuleViolation(_messageFactory.Invoke(obj, value) ?? RuleViolations.General);
    }
  }
}
